<?php
/**
 * 
 * Help developer authorize user login with some roles to access action.
 * @author Windy
 *
 */
class Helper_Authorization{
	/**
	 * 
	 * Function to authorize 
	 * @param array $roles String array of roles name. Default = null
	 */
	public static function authorize($authorizedRoles = null){
		
		$auth = Zend_Auth::getInstance();
		if($auth->hasIdentity()){
			if($authorizedRoles != null){
				$arrAuthorizedRoles = (array) $authorizedRoles;
				$identity = $auth->getIdentity();
				$user = Doctrine::getTable("Entity_BaseUser")->find($identity->id);
				$arrUserRoles = $user->UserRoles;
				foreach ($arrUserRoles as $userRole){
					$arrRoles[] = $userRole->Role->name;
				}
				$matched = 0;
				foreach ($arrAuthorizedRoles as $authorizedRole) {
					if(in_array($authorizedRole, $arrRoles)){
						$matched+=1;
					}
				}
				if($matched!=0){
					return Constain_Authorization::AUTHORIZED;
				}else{
					return Constain_Authorization::NOT_AUTHORIZED;
				}
			}else{
				return Constain_Authorization::AUTHORIZED;
			}
		} else{
			return Constain_Authorization::NOT_LOGIN;
		}
	}
}
?>